﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Telerik.Windows.Controls;
using DunSilverlight.Comm;
using DunSilverlight.ViewModel;

namespace DunSilverlight.Views
{
    public partial class RightForm : UserControl
    {
        RightService.RightServiceClient ser = new RightService.RightServiceClient();

        RightModel obj = new RightModel();
        public RightForm()
        {
            InitializeComponent();
            ser.SaveOrUpdateCompleted+=new EventHandler<RightService.SaveOrUpdateCompletedEventArgs>(ser_SaveOrUpdateCompleted);
            this.DataContext = obj;
        }

        public RightForm(DunLibrary.User.Right right) 
        {
            InitializeComponent();
            this.obj = RightModel.Create(right);
            ser.SaveOrUpdateCompleted += new EventHandler<RightService.SaveOrUpdateCompletedEventArgs>(ser_SaveOrUpdateCompleted);
            this.DataContext = obj;
        }

        void ser_SaveOrUpdateCompleted(object sender, RightService.SaveOrUpdateCompletedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (obj.Validation())
            {
                var entity = obj.ToEntity();
                ser.SaveOrUpdateAsync(JsonSerializerHelper.EntityToJson(entity));
            }
            else
            {
                Tools.ShowMessage("输入格式不正确", "", false);
            }
            
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            (this.Parent as RadWindow).Close();
        }


    }
}
